package com.baidu.controller;

import com.baidu.common.Rest;
import com.baidu.entity.SysUser;
import com.baidu.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DemoController {
    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/select")
    public String select(){
        List<SysUser> objects = sysUserService.selectList();
        return objects.toString();
    }

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    // GET @RequestParam
    // POST @RequestBody
    public Rest register(
            @RequestParam() String username,
            @RequestParam() String password,
            @RequestParam(required = false) String email,
            @RequestParam(required = false) Integer sex,
            @RequestParam() String phone
    ){
        System.out.println("这是GET方法");
        SysUser user = new SysUser();
        user.setUsername(username);
        user.setEmail(email);
        user.setPassword(password);
        user.setSex(sex);
        user.setPhone(phone);
        return sysUserService.register(user);
    }

    @RequestMapping(value = "/postlist",method = RequestMethod.POST)
    public Rest registerByPost(@RequestBody SysUser user){
        System.out.println("这是POST方法");
        if (user.getUsername() == null){
            return Rest.faild("用户名不能为空");
        }
        if (user.getPassword() == null){
            return Rest.faild("密码不能为空");
        }
        return sysUserService.register(user);
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public Rest deleteById(@PathVariable() Integer id){
        System.out.println(id);
        return sysUserService.destster(id);
    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Rest updateUsernameById(@RequestBody SysUser user){
        System.out.println("这是PUT方法");
        return sysUserService.updataster(user);
    }


}
